<?php

class RemoteController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->renderFile('index.tpl');
	}

	public function actionNew()
	{
		$pics = Yii::app()->request->getParam('pics');
		$modPicture = new Picture();
		$arrPictureList = json_decode($pics, true);
		foreach ($arrPictureList as $key => $item) {
			$picture = array(
				"pic_show_url" => $item["pic_show_url"],
				"pic_url" => $item["pic_url"],
				"pic_key" => $item["pic_key"],
				"source" => $item["source"],
				"click_url" => $item["click_url"],
				"price" => $item["price"],
				"name" => $item["name"],
				"profit" => $item["profit"],
				"add_time" => strtotime("now"),
				"local_path" => $item["local_path"],
				"uniq_sign" => $item["uniq_sign"],
			);
			$item['rate_count'] && ($picture['rate_count'] = $item['rate_count']);
			$item['bak_id'] && ($picture['bak_id'] = $item['bak_id']);
			$item['score'] && ($picture['score'] = $item['score']);
			$modPicture->save($picture);
		}
		//$modelItem = new Item();
		//$modelItem->save(array('key'=>$key, 'data' => $data));
		echo "OK";
	}

	public function actionGetItems()
	{
		$callback = Yii::app()->request->getParam('cb');
		$arrTplData = array();
		$modelItem = new Item();
		$arrItemList = $modelItem->getList();
		$arrTplData['arrItemList'] = $arrItemList;

		$arrJsonData = array();
		foreach ($arrItemList as $key => $item) {
			$arrJsonData[] = json_decode($item['data']);
		}
		echo $callback . '(' .json_encode($arrJsonData) . ')';
		$this->renderFile('items.tpl', $arrTplData);
	}
}